package com.innovate.nms.view;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.innovate.nms.domain.SmsConfig;
import com.innovate.nms.service.channel.SmsChannel;
import com.ivan.toolkit.swing.ConfigDialog;
import com.ivan.toolkit.swing.PromptTextField;

public class WirelessConfigDialog extends ConfigDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2724121355647202614L;
	
	private final JPanel contentPanel = new JPanel();
	private JCheckBox chckbxAutoDetect;
	private JComboBox<String> comboBoxPort;
	private JComboBox<Integer> comboBoxBaudrate;
	private JComboBox<NamedValue<Integer>> comboBoxParity;
	private JComboBox<Integer> comboBoxDatabits;
	private JComboBox<NamedValue<Integer>> comboBoxStopbits;
	private PromptTextField tfTimeout;
	private PromptTextField tfInterval;
	private JComboBox<String> comboBoxProtocol;
	private JComboBox<String> comboBoxLeader;

	/**
	 * Create the dialog.
	 */
	public WirelessConfigDialog() {
		setTitle("通信配置");
		setBounds(100, 100, 427, 424);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{10, 0, 185, 10, 0};
		gbl_contentPanel.rowHeights = new int[]{20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel label = new JLabel("自动识别：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 1;
			contentPanel.add(label, gbc_label);
		}
		{
			chckbxAutoDetect = new JCheckBox("自动识别串口");
			chckbxAutoDetect.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					updateControls();
				}
			});
			GridBagConstraints gbc_chckbxAutoDetect = new GridBagConstraints();
			gbc_chckbxAutoDetect.anchor = GridBagConstraints.WEST;
			gbc_chckbxAutoDetect.insets = new Insets(0, 0, 5, 5);
			gbc_chckbxAutoDetect.gridx = 2;
			gbc_chckbxAutoDetect.gridy = 1;
			contentPanel.add(chckbxAutoDetect, gbc_chckbxAutoDetect);
		}
		{
			JLabel label = new JLabel("端口：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 2;
			contentPanel.add(label, gbc_label);
		}
		{
			comboBoxPort = new JComboBox<String>();
			GridBagConstraints gbc_comboBoxPort = new GridBagConstraints();
			gbc_comboBoxPort.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxPort.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxPort.gridx = 2;
			gbc_comboBoxPort.gridy = 2;
			contentPanel.add(comboBoxPort, gbc_comboBoxPort);
		}
		{
			JLabel label = new JLabel("通信协议：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 3;
			contentPanel.add(label, gbc_label);
		}
		{
			comboBoxProtocol = new JComboBox<String>();
			comboBoxProtocol.setModel(new DefaultComboBoxModel<String>(new String[] {"F2003", "JYC331"}));
			GridBagConstraints gbc_comboBoxProtocol = new GridBagConstraints();
			gbc_comboBoxProtocol.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxProtocol.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxProtocol.gridx = 2;
			gbc_comboBoxProtocol.gridy = 3;
			contentPanel.add(comboBoxProtocol, gbc_comboBoxProtocol);
		}
		{
			JLabel label = new JLabel("引导符：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 4;
			contentPanel.add(label, gbc_label);
		}
		{
			comboBoxLeader = new JComboBox<String>();
			comboBoxLeader.setModel(new DefaultComboBoxModel<String>(new String[] {"@", "#"}));
			comboBoxLeader.setSelectedIndex(0);
			GridBagConstraints gbc_comboBoxLeader = new GridBagConstraints();
			gbc_comboBoxLeader.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxLeader.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxLeader.gridx = 2;
			gbc_comboBoxLeader.gridy = 4;
			contentPanel.add(comboBoxLeader, gbc_comboBoxLeader);
		}
		{
			JLabel label = new JLabel("波特率：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 5;
			contentPanel.add(label, gbc_label);
		}
		{
			comboBoxBaudrate = new JComboBox<Integer>();
			comboBoxBaudrate.addItem(2400);
			comboBoxBaudrate.addItem(9600);
			comboBoxBaudrate.addItem(19200);
			comboBoxBaudrate.addItem(57600);
			comboBoxBaudrate.addItem(115200);
			GridBagConstraints gbc_comboBoxBaudrate = new GridBagConstraints();
			gbc_comboBoxBaudrate.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxBaudrate.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxBaudrate.gridx = 2;
			gbc_comboBoxBaudrate.gridy = 5;
			contentPanel.add(comboBoxBaudrate, gbc_comboBoxBaudrate);
		}
		{
			JLabel label = new JLabel("校验位：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 6;
			contentPanel.add(label, gbc_label);
		}
		{
			comboBoxParity = new JComboBox<NamedValue<Integer>>();
			comboBoxParity.addItem(new NamedValue<Integer>("NONE", 0));
			comboBoxParity.addItem(new NamedValue<Integer>("ODD", 1));
			comboBoxParity.addItem(new NamedValue<Integer>("EVEN", 2));
			comboBoxParity.addItem(new NamedValue<Integer>("MARK", 3));
			comboBoxParity.addItem(new NamedValue<Integer>("SPACE", 4));
			GridBagConstraints gbc_comboBoxParity = new GridBagConstraints();
			gbc_comboBoxParity.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxParity.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxParity.gridx = 2;
			gbc_comboBoxParity.gridy = 6;
			contentPanel.add(comboBoxParity, gbc_comboBoxParity);
		}
		{
			JLabel label = new JLabel("数据位：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 7;
			contentPanel.add(label, gbc_label);
		}
		{
			comboBoxDatabits = new JComboBox<Integer>();
			comboBoxDatabits.addItem(5);
			comboBoxDatabits.addItem(6);
			comboBoxDatabits.addItem(7);
			comboBoxDatabits.addItem(8);
			GridBagConstraints gbc_comboBoxDatabits = new GridBagConstraints();
			gbc_comboBoxDatabits.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxDatabits.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxDatabits.gridx = 2;
			gbc_comboBoxDatabits.gridy = 7;
			contentPanel.add(comboBoxDatabits, gbc_comboBoxDatabits);
		}
		{
			JLabel label = new JLabel("停止位：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.gridx = 1;
			gbc_label.gridy = 8;
			contentPanel.add(label, gbc_label);
		}
		{
			comboBoxStopbits = new JComboBox<NamedValue<Integer>>();
			comboBoxStopbits.addItem(new NamedValue<Integer>("1", 1));
			comboBoxStopbits.addItem(new NamedValue<Integer>("2", 2));
			comboBoxStopbits.addItem(new NamedValue<Integer>("1.5", 3));
			GridBagConstraints gbc_comboBoxStopbits = new GridBagConstraints();
			gbc_comboBoxStopbits.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxStopbits.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxStopbits.gridx = 2;
			gbc_comboBoxStopbits.gridy = 8;
			contentPanel.add(comboBoxStopbits, gbc_comboBoxStopbits);
		}
		{
			JLabel label = new JLabel("接收超时：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 9;
			contentPanel.add(label, gbc_label);
		}
		{
			tfTimeout = new PromptTextField();
			GridBagConstraints gbc_tfTimeout = new GridBagConstraints();
			gbc_tfTimeout.insets = new Insets(0, 0, 5, 5);
			gbc_tfTimeout.fill = GridBagConstraints.HORIZONTAL;
			gbc_tfTimeout.gridx = 2;
			gbc_tfTimeout.gridy = 9;
			contentPanel.add(tfTimeout, gbc_tfTimeout);
		}
		{
			JLabel label = new JLabel("数据间隔：");
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.insets = new Insets(0, 0, 0, 5);
			gbc_label.gridx = 1;
			gbc_label.gridy = 10;
			contentPanel.add(label, gbc_label);
		}
		{
			tfInterval = new PromptTextField();
			tfInterval.setText("30000");
			GridBagConstraints gbc_tfInterval = new GridBagConstraints();
			gbc_tfInterval.insets = new Insets(0, 0, 0, 5);
			gbc_tfInterval.fill = GridBagConstraints.HORIZONTAL;
			gbc_tfInterval.gridx = 2;
			gbc_tfInterval.gridy = 10;
			contentPanel.add(tfInterval, gbc_tfInterval);
		}
		
		initValues();
	}
	
	private void initValues() {
		
		SmsConfig config = SmsConfig.instance.get();
		
		List<String> names = SmsChannel.getPortNames();
		if(!names.isEmpty()) {
			names.stream().forEach(name -> {
				this.comboBoxPort.addItem(name);
			});
			this.comboBoxPort.setSelectedIndex(0);
		}
		this.chckbxAutoDetect.setSelected(config.isAutoDetect());
		this.comboBoxPort.setSelectedItem(config.getPort());
		this.comboBoxProtocol.setSelectedIndex(config.getProtocol());
		this.comboBoxLeader.setSelectedItem(config.getLeader());
		this.comboBoxBaudrate.setSelectedItem(config.getBaudrate());
		this.comboBoxParity.setSelectedItem(new NamedValue<Integer>("", config.getParity()));
		this.comboBoxDatabits.setSelectedItem(config.getDatabits());
		this.comboBoxStopbits.setSelectedItem(new NamedValue<Integer>("", config.getStopbits()));
		this.tfTimeout.setText(Integer.toString(config.getTimeout()));
		this.tfInterval.setText(Integer.toString(config.getWaitInterval()));
		
		updateControls();
	}
	
	private void updateControls() {
		comboBoxPort.setEnabled(!chckbxAutoDetect.isSelected());
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doCommit() throws Exception {
		
		boolean autoDetect = this.chckbxAutoDetect.isSelected();
		
		String portName = (String)this.comboBoxPort.getSelectedItem();
		
		int protocol = this.comboBoxProtocol.getSelectedIndex();
		
		String leader = (String)this.comboBoxLeader.getSelectedItem();
		
		int baudrate = (int)this.comboBoxBaudrate.getSelectedItem();
		NamedValue<Integer> p = (NamedValue<Integer>)this.comboBoxParity.getSelectedItem();
		int parity = p.getValue();
		
		int databits = (int)this.comboBoxDatabits.getSelectedItem();
		NamedValue<Integer> s = (NamedValue<Integer>)this.comboBoxStopbits.getSelectedItem();
		int stopbits = s.getValue();
		
		int timeout = this.toInteger(tfTimeout, "无效的超时时间");
		int interval = this.toInteger(tfInterval, "无效的接收间隔");
		
		SmsConfig config = SmsConfig.instance.get();
		config.setAutoDetect(autoDetect);
		config.setPort(portName);
		config.setProtocol(protocol);
		config.setLeader(leader);
		config.setBaudrate(baudrate);
		config.setParity(parity);
		config.setDatabits(databits);
		config.setStopbits(stopbits);
		config.setTimeout(timeout);
		config.setWaitInterval(interval);
		SmsConfig.instance.set();
		
	}

	
	
}
